import 'package:android_dynamic_icon/android_dynamic_icon.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:pornhub_app/views/mine/frontpage/change_icon_pop.dart';

import '../../../utils/color.dart';

class HideAppsPage extends StatefulWidget {
  const HideAppsPage({super.key});

  @override
  State<StatefulWidget> createState() => _HideAppsPage();
}

class _HideAppsPage extends State<HideAppsPage> {
  bool show = false;
  final _androidDynamicIconPlugin = AndroidDynamicIcon();
  final List<String> iconList = [
    'MainActivity',
    'IconOne',
    'IconTwo',
    'IconThree',
    'IconFour',
    'IconFive',
  ];

  void onCLick(int index) {
    if (index == 10) {
      setState(() {
        show = !show;
      });
      return;
    }
    SmartDialog.showLoading(
      msg: "",
      animationBuilder: (controller, child, animationParam) {
        return ChangeIconPop(
          tap: () {
            _androidDynamicIconPlugin
                .changeIcon(classNames: [iconList[index], '']);
            SmartDialog.dismiss();
          },
        );
      },
      alignment: Alignment.center,
    );
  }

  @override
  void initState() {
    AndroidDynamicIcon.initialize(classNames: iconList);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w),
      padding:
          EdgeInsets.only(left: 16.w, right: 16.w, top: 11.w, bottom: 11.w),
      width: double.maxFinite,
      decoration: BoxDecoration(
        color: COLOR.hexColor('#ffffff'),
        borderRadius: BorderRadius.circular(6.0),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          InkWell(
            highlightColor: Colors.white,
            splashColor: Colors.white,
            onTap: () => onCLick(10),
            child: Flex(
              direction: Axis.horizontal,
              children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    "隐藏APP",
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: COLOR.hexColor("#666666"),
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
                FittedBox(
                  child: Image(
                    image: AssetImage(show
                        ? "assets/mine/img_hide_apps_hide.png"
                        : "assets/mine/img_hide_apps_more.png"),
                    width: show ? 13.w : 8.w,
                    height: show ? 8.w : 13.w,
                    fit: BoxFit.contain,
                  ),
                ),
              ],
            ),
          ),
          Visibility(
            visible: show,
            child: Container(
              padding: EdgeInsets.only(top: 14.w),
              width: double.maxFinite,
              child: Flex(
                direction: Axis.horizontal,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () => onCLick(1),
                      child: _buildImgText(
                          "assets/mine/img_change_desktop_one.png", "计算器"),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () => onCLick(2),
                      child: _buildImgText(
                          "assets/mine/img_change_desktop_two.png", "网易云"),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () => onCLick(3),
                      child: _buildImgText(
                          "assets/mine/img_change_desktop_three.png", "球球英雄"),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () => onCLick(4),
                      child: _buildImgText(
                          "assets/mine/img_change_desktop_four.png", "图片处理"),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () => onCLick(5),
                      child: _buildImgText(
                          "assets/mine/img_change_desktop_five.png", "第五人格"),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildImgText(String imgSource, String title) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Image.asset(
          imgSource,
          width: 50.w,
          height: 50.w,
        ),
        const Padding(padding: EdgeInsets.only(top: 4)),
        Text(
          title,
          style: TextStyle(
            fontSize: 11.sp,
            color: COLOR.hexColor('#666666'),
            fontWeight: FontWeight.w600,
          ),
        ),
      ],
    );
  }
}
